﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Controls;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

namespace TouchUmlUI
{
    public class AkcjaObj : AssociableObj
    {

        Ellipse ellipse;
        TextBlock tb = new TextBlock();
        Rectangle r = new Rectangle();

        public override void setX(int ax)
        {
            InkCanvas.SetLeft(guml.panel, ax);
        }


        public override void setY(int ay)
        {
            InkCanvas.SetTop(guml.panel, ay);
        }


        public override void showHints(bool a_show)
        {
            int thick = a_show ? 1 : 0;
            r.StrokeThickness = thick;
        }


        public override void Refresh()
        {
            tb.Text = guml.obj_uml.Nazwa;
            guml.panel.UpdateLayout();
            tb.UpdateLayout();
            updateDependencyProperties();
        }

        public override void updateDependencyProperties()
        {            
            base.updateDependencyProperties();               
           // base.calculateNewPoints();
            double lx = (getWidth() - tb.ActualWidth) / 2.0f;
            double ly = (getHeight() - tb.ActualHeight) / 2.0f;
            Canvas.SetLeft(tb, lx);
            Canvas.SetTop(tb, ly);            
            //
            r.Width = tb.ActualWidth + 10;
            r.Height = tb.ActualHeight + 10;
            Canvas.SetLeft(r, lx - 5);
            Canvas.SetTop(r, ly - 5);           
        }

        public AkcjaObj(String a_title, int a_width, int a_height, int a_left, int a_top, OurCanvas a_canvas,int a_id, GUML guml)
            : base(guml)
        {
            guml.canvas = a_canvas;

            if (a_width < 0) { a_left = a_left + a_width; a_width = -a_width; }
            if (a_height < 0) { a_top = a_top + a_height; a_height = -a_height; }

            x = a_left;
            y = a_top;
            width = a_width;
            height = a_height;

            guml.panel = new Canvas();
            guml.panel.Width = width;
            guml.panel.Height = height;
            //guml.panel.Background = Brushes.Red;
            InkCanvas.SetLeft(guml.panel, x);
            InkCanvas.SetTop(guml.panel, y);

            LinearGradientBrush lgb = new LinearGradientBrush();
            lgb.GradientStops.Add(new GradientStop(Color.FromRgb(181,177,187),0.0));
            lgb.GradientStops.Add(new GradientStop(Color.FromRgb(228, 222, 236), 1.0));
            lgb.StartPoint = new Point(0,0);
            lgb.EndPoint = new Point(0, 1.0);
            ellipse = new Ellipse();
            ellipse.Fill = lgb;
            ellipse.Width = width;
            ellipse.Height = height;
            ellipse.Stroke = Brushes.Black;
            ellipse.StrokeThickness = 1;
            guml.panel.Children.Add(ellipse);            
            a_canvas.Children.Add(guml.panel);            
            tb.Text = a_title;
            tb.SizeChanged += new SizeChangedEventHandler(tb_SizeChanged);
            //tb.Background = Brushes.Red;            
            tb.MouseUp += tb_MouseDown;
            Binding bind1 = new Binding("ActualWidthProperty");
            bind1.Source = guml.panel;
            bind1.Path = new PropertyPath(Canvas.ActualWidthProperty);
            Binding bind2 = new Binding("ActualHeightProperty");
            bind2.Source = guml.panel;
            bind2.Path = new PropertyPath(Canvas.ActualHeightProperty);
            ellipse.SetBinding(Ellipse.WidthProperty, bind1);
            ellipse.SetBinding(Ellipse.HeightProperty, bind2);

            r.Stroke = Brushes.Black;
            r.StrokeThickness = 1;
            r.StrokeDashArray.Add(5);
            r.Fill = new SolidColorBrush(Color.FromArgb(10, 255, 255, 255));
            r.SnapsToDevicePixels = true;
            r.MouseDown += tb_MouseDown;
            r.Opacity = 0.5;

            guml.panel.Children.Add(r);
            guml.panel.Children.Add(tb);

            guml.canvas.UpdateLayout();
            guml.panel.UpdateLayout();
            updateDependencyProperties();
        }

        void tb_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            // base.calculateNewPoints();
            double lx = (getWidth() - tb.ActualWidth) / 2.0f;
            double ly = (getHeight() - tb.ActualHeight) / 2.0f;
            Canvas.SetLeft(tb, lx);
            Canvas.SetTop(tb, ly);
            //
            r.Width = tb.ActualWidth + 10;
            r.Height = tb.ActualHeight + 10;
            Canvas.SetLeft(r, lx - 5);
            Canvas.SetTop(r, ly - 5);   
        }
      
        void tb_MouseDown(object sender, MouseEventArgs e)
        {
            if (SignalR.windowParent.handWrite != null) return;
            SignalR.windowParent.handWrite = new UCUTextRecognizer.HandWrite();            
            UCUTextRecognizer.HandWrite handWrite = SignalR.windowParent.handWrite;
            handWrite.setExistingText(guml.obj_uml.Nazwa);
            handWrite.InitializeComponent();


            try
            {
                if (!tb.Text.StartsWith("Akcja"))
                {
                    handWrite.setExistingText(tb.Text);
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            
            double sy = getY() - 90;            
            double nh = 180;
            double sx = SignalR.windowParent.sv.HorizontalOffset + 50;
            double nw = SignalR.windowParent.sv.ActualWidth - 100;            

            handWrite.Width = nw;
            handWrite.Height = nh;
            InkCanvas.SetLeft(handWrite,sx);
            InkCanvas.SetTop(handWrite, sy);
            handWrite.Opacity = 1.0f;
            //
            handWrite.bindWithObject(guml.obj_uml, guml.canvas, guml.umlobj);                        
            SignalR.windowParent.active_canvas.Children.Add(handWrite);
            e.Handled = true;
        }

        public override void setWidth(int a_width)
        {
            width = a_width;
        }

        public override void setHeight(int a_height)
        {
            height = a_height;
        }

    }
}
